package cade;

import evaluator.FitnessFunction;
import evaluator.ServerCommunication;
import evaluator.TimeOverException;

public class Individuo implements Comparable<Individuo> {
	public double x[];
	public double f;
	public FitnessFunction funcao;
	
	public Individuo(int qtdVariaveis, FitnessFunction funcao){
		x = new double[qtdVariaveis];
		this.funcao = funcao;
	}
	
	public void avaliar(ServerCommunication server) throws Exception {
		try {
			f = (Double) server.launchSimulation(x, 600);
		} catch(TimeOverException e) {
			System.exit(-1);//TODO: Verificar necessidade
		}
	}

	@Override
	public int compareTo(Individuo o) {
		double compareFitness = ((Individuo) o).f; 
		int ret;
		
		if(this.f == compareFitness)
			ret = 0;
		else
			ret = (this.f > compareFitness) ? 1 : -1;
		
		//ascending order
		return ret;
 
		//descending order
		//return compareQuantity - this.quantity;
	}
}
